package demo1215.pm;

import java.util.Scanner;

/*
学生管理系统(2.0):
1.添加学生信息
2.查询学生信息
3.修改学生信息
4.删除学生信息

 把学生信息添加StringBuilder对象中
 添加的格式:"101,tom,18;102,jack,19;"
 */
public class StudentManager {
    static Scanner scan = new Scanner(System.in);
    public static void main(String[] args) {
        //创建一个StringBuilder 对象
        StringBuilder s = new StringBuilder();
        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择以下操作:1.添加2.查询3.修改4.删除");
            String num = scan.next();
            switch (num) {
                case "1":
                    add(s);
                    break;
                case "2":
                    findAll(s);
                    break;
                case "3":
                    modify(s);
                    break;
                case "4":
                   delete(s);
                    break;
                case "0":
                    System.out.println("退出程序");
                    return;
            }

        }
    }

    /**
     * 删除方法
     * "101,tom,18;102,jack,19;"
     * 思路:
     * 1.
     * @param s
     */
    private static void delete(StringBuilder s) {
        System.out.println("请输入删除学生的学号:");
        String stuId = scan.next();
        int index = s.indexOf(" "+stuId+" ");
        if (index==-1){
            System.out.println("删除学生不存在,删除失败!");
            return;
        }
        int end = s.indexOf(";", index);
        s.delete(index,end+1);
        System.out.println("删除成功!");
    }

    /**
     * 修改学生信息
     * "101,tom,18;102,jack,19;"
     * 思路:
     * 查询学号是否存在
     * 1.不存在,修改失败!
     * 2.存在,接着录入姓名和年龄
     *
     * @param s
     */
    private static void modify(StringBuilder s) {
        System.out.println("请输入修改的学生学号:");
        String stuId = scan.next();
        int index = s.indexOf(" "+stuId+" ");
        if (index == -1) {
            System.out.println("修改失败!");
            return;
        }
        System.out.println("请输入修改的学生姓名:");
        String name = scan.next();
        System.out.println("请输入修改的学生年龄:");
        String age = scan.next();
        String s1 = " "+stuId + " ," + name + "," + age;
        int end = s.indexOf(";", index);
        s.replace(index,end,s1);//"123" 123
    }

    /**
     * 查询学生信息
     * "101,tom,18;102,jack,19;"
     *
     * @param s
     */
    private static void findAll(StringBuilder s) {
        String[] arr = s.toString().split(";");
        System.out.println("学号\t姓名\t年龄");
        for (int i = 0; i < arr.length; i++) {
            //102,jack,19
            String[] stu = arr[i].split(",");
            System.out.println(stu[0] + "\t" + stu[1] + "\t" + stu[2]);
        }
    }

    /**
     * 添加学生信息
     * 添加的格式:"101,tom,18;"
     * 添加信息的学号不能添加重复
     *
     * @param s
     */
    private static void add(StringBuilder s) {
        System.out.println("添加学生信息");
        System.out.println("请输入学号:");
        String stuId = scan.next();
        //判断学号是否重复
        if (s.indexOf(" "+stuId+" ") != -1) {
            System.out.println("学号已经存在,添加失败");
            return;
        }
        System.out.println("请输入姓名:");
        String name = scan.next();
        System.out.println("请输入年龄:");
        String age = scan.next();
        String s1 = " "+stuId + " ," + name + "," + age + ";";
        s.append(s1);
        System.out.println("添加成功!");
    }


}
